var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusType: ["全部", "待付款", "待发货", "待收货", "待评价"],
    currentType: 0,
    tabClass: ["", "", "", "", ""],
    loadingMoreHidden: false,
    currPage: 1,
    orderListTwo: [],
    oid: 0,
    tempFilePaths: [],
    fileName: [],
    pics: [],
    picimg1: "",
    picimg2: "",
    picimg3: "",
    picimg4: "",
    countStar: 5,
    chooseid: ""
  },
  /**
   * 提交评价
   */
  bindFormSubmit: function (e) {
    var pid = e.detail.target.dataset.recordid;
    var countStar = this.Getstar(this.data.orderListTwo, pid)
    this.setData({
      countStar: countStar
    })
    var content = e.detail.value.textarea;
    var recordid = e.detail.target.dataset.recordid;
    if (this.data.pics.length > 0) {//选择图片
      this.evaluate(content, this.data.oid, recordid)
    } else {//未选择图片
      this.submintEval(content, this.data.oid, recordid)
    }
  },
  //获取评分等级
  Getstar: function (arr, pid) {
    var count = 0
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].RecordId == pid) {
        if (arr[i].star1 == 1) {
          count++
        }
        if (arr[i].star2 == 1) {
          count++
        }
        if (arr[i].star3 == 1) {
          count++
        }
        if (arr[i].star4 == 1) {
          count++
        }
        if (arr[i].star5 == 1) {
          count++
        }
      }
    }
    return count
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onload2");
    this.data.oid = options.oid
    this.onlist(options.oid);
  },
  //星星评分
  showStar: function (e) {
    var that = this;
    var leveid = e.target.dataset.leveid
    var pid = e.target.dataset.pid
    var orderListTwo = that.data.orderListTwo
    for (var j = 0; j < orderListTwo.length; j++) {
      if (pid == orderListTwo[j].RecordId) {
        if (leveid == 1) {
          if (orderListTwo[j].star1 == 0) {
            orderListTwo[j].star1 = 1
          }
          else {
            orderListTwo[j].star2 = 0
            orderListTwo[j].star3 = 0
            orderListTwo[j].star4 = 0
            orderListTwo[j].star5 = 0
          }
        }
        else if (leveid == 2) {
          if (orderListTwo[j].star2 == 0) {
            orderListTwo[j].star1 = 1
            orderListTwo[j].star2 = 1
          } else {
            orderListTwo[j].star3 = 0
            orderListTwo[j].star4 = 0
            orderListTwo[j].star5 = 0
          }
        }
        else if (leveid == 3) {
          if (orderListTwo[j].star3 == 0) {
            orderListTwo[j].star1 = 1
            orderListTwo[j].star2 = 1
            orderListTwo[j].star3 = 1
          } else {
            orderListTwo[j].star4 = 0
            orderListTwo[j].star5 = 0
          }
        }
        else if (leveid == 4) {
          if (orderListTwo[j].star4 == 0) {
            orderListTwo[j].star1 = 1
            orderListTwo[j].star2 = 1
            orderListTwo[j].star3 = 1
            orderListTwo[j].star4 = 1
          } else {
            orderListTwo[j].star5 = 0
          }
        }
        else if (leveid == 5) {
          if (orderListTwo[j].star4 == 0) {
            orderListTwo[j].star1 = 1
            orderListTwo[j].star2 = 1
            orderListTwo[j].star3 = 1
            orderListTwo[j].star4 = 1
            orderListTwo[j].star5 = 1
          } else {
            orderListTwo[j].star5 = 1
          }
        }
      }
    }
    that.setData({
      orderListTwo: orderListTwo
    })
  },

  onlist: function (oid) {
    // 获取订单列表
    wx.showLoading();
    var that = this;
    var postDatalist = {
      uid: app.globalData.userInfo.Uid,
      page: that.data.currPage,
      orderState: 120
    };
    wx.request({
      url: app.globalData.host + '/api/order/GetUserOrders',
      data: postDatalist,
      success: function (res) {
        wx.hideLoading();
        var orderListTwos = [];
        if (res.data.Code == 0) {
          var orderList = res.data.Data.OrderList;
          for (var i = 0; i < orderList.length; i++) {
            for (var j = 0; j < orderList[i].ProductInfos.length; j++) {
              if (orderList[i].ProductInfos[j] && orderList[i].Oid == oid) {
                var c = {}
                c.ShowImg = app.globalData.image190_190host + orderList[i].ProductInfos[j].ShowImg;
                c.Name = orderList[i].ProductInfos[j].Name;
                c.Oid = orderList[i].ProductInfos[j].Oid;
                c.RecordId = orderList[i].ProductInfos[j].RecordId;
                c.IsReview = orderList[i].ProductInfos[j].IsReview;
                c.Pid = orderList[i].ProductInfos[j].Pid;
                c.pic1 = "/images/nav/star.png";
                c.pic2 = "/images/nav/star2.png";
                c.star1 = 0;
                c.star2 = 0;
                c.star3 = 0;
                c.star4 = 0;
                c.star5 = 0;
                orderListTwos.push(c)
              }

            }
          }
          that.setData({
            orderListTwo: orderListTwos,
            loadingMoreHidden: res.data.Data.PageModel.HasNextPage
          });
        }
      }
    })

  },
  loadMorelist: function (e) {
    var that = this;
    if (!that.data.loadingMoreHidden) return;
    var currPage = that.data.CurrentPage;
    that.setData({
      CurrentPage: currPage + 1
    })
    that.onlist(oid);
  },

  choose: function (e) {//这里是选取图片的方法
    var that = this;
    var chooseid = e.currentTarget.dataset.chooseid
    wx.chooseImage({
      count: 4, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        that.setData({
          pics: imgsrc,
          chooseid: chooseid
        });
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  evaluate: function (content, oid, recordid) {//这里触发图片上传的方法
    var that = this;
    var pics = that.data.pics;
    that.uploadimg({
      url: app.globalData.host + '/api/tool/upload?operation=uploaduserreview',//这里是你图片上传的接口
      path: pics//这里是选取的图片的地址数组
    });
    setTimeout(function () {
      that.submintEval(content, oid, recordid)
    }, 2000)
  },
  //多张图片上传
  uploadimg: function (data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'fileData',
      formData: null,
      success: (resp) => {
        success++;
        console.log(resp.data)
        var str = resp.data
        var pic = JSON.parse(str);
        var pic_name = pic.Data;
        console.log(i);
        if (i == 0) {
          that.setData({
            picimg1: pic_name
          })
        }
        if (i == 1) {
          that.setData({
            picimg2: pic_name
          })
        }
        if (i == 2) {
          that.setData({
            picimg3: pic_name
          })
        }
        if (i == 3) {
          that.setData({
            picimg4: pic_name
          })
        }
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++;
        if (i == data.path.length) {  //当图片传完时，停止调用     
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
        } else {//若图片还没有传完，则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }

      }
    });
  },
  submintEval: function (content, oid, recordid) {//评价提交
    var that = this;
    if (content == "" || content == null) {
      wx.showModal({
        title: '提示',
        content: "请填写评价内容",
        showCancel: false
      })
      return
    }
    if (that.data.picimg1 !== "") {
      var picname1 = '&pic1=' + that.data.picimg1
      console.log(picname1)
    }
    else {
      var picname1 = ""
    }
    if (that.data.picimg2 !== "") {
      var picname2 = '&pic2=' + that.data.picimg2
      console.log(picname2)
    } else {
      var picname2 = ""
    }
    if (that.data.picimg3 !== "") {
      var picname3 = '&pic3=' + that.data.picimg3
      console.log(picname3)
    } else {
      var picname3 = ""
    }
    if (that.data.picimg4 !== "") {
      var picname4 = '&pic4=' + that.data.picimg4
      console.log(picname4)
    } else {
      var picname4 = ""
    }
    wx.request({//评论提交
      url: app.globalData.host + '/api/user/reviewproduct?oid=' + oid + '&recordid=' + recordid + '&message=' + content + picname1 + picname2 + picname3 + picname4 + '&star=' + that.data.countStar,
      success: function (res) {
        console.log("提交成功")
        console.log(res)
        if (res.data.Code == 0) {
          wx.showToast({
            title: "提交成功",
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      },
      fail: function (erro) {
        console.log(erro)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})